package com.yztc.orangeentertainment.ui.main.findfragment.findadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.findbean.FindDbBean;
import com.yztc.orangeentertainment.ui.main.findfragment.findmain.FindHotSearchFragment;
import com.yztc.orangeentertainment.ui.main.findfragment.findmain.FindSearchActivity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/10.
 */
public class FindSearchRlvAdapter extends RecyclerView.Adapter<FindSearchRlvAdapter
        .FindSearchViewHolder> {
    private Context context;
    private List<FindDbBean> list;

    public FindSearchRlvAdapter(Context context, List<FindDbBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public FindSearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        FindSearchViewHolder holder = new FindSearchViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.findhotsearch_listview, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(FindSearchViewHolder holder, final int position) {
        holder.findhotsearchStr.setText(list.get(position).getTitle());
        holder.findhotsearchLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindSearchActivity.findsearchEdt.setText(list.get(position).getTitle());
            }
        });
        holder.findhotsearchDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FindSearchActivity.dbUtils.delete(FindDbBean.class, WhereBuilder.b("title",
                            "=", list.get(position).getTitle()));
                    FindHotSearchFragment.list.clear();
                    if (FindSearchActivity.dbUtils.findAll(FindDbBean.class).size() == 0) {
                        FindHotSearchFragment.footviewText.setVisibility(View.GONE);
                    }
                    list.addAll(FindSearchActivity.dbUtils.findAll(FindDbBean.class));
                    FindHotSearchFragment.adapter.notifyDataSetChanged();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class FindSearchViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.findhotsearch_str)
        TextView findhotsearchStr;
        @InjectView(R.id.findhotsearch_linear)
        LinearLayout findhotsearchLinear;
        @InjectView(R.id.findhotsearch_del)
        ImageView findhotsearchDel;

        public FindSearchViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }
}
